﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class OrderItemEditControl : UserControl
    {
        private IOrderItem _orderItem;
        private Workspace _workspace;

        public OrderItemEditControl(IOrderItem orderItem, Workspace workspace)
        {
            _workspace = workspace;
            cbxOrderPart.DataSource = _workspace.GetAllOrderParts();
            cbxPriceItem.DataSource = _workspace.GetAllPriceItems();

            OrderItem = orderItem;

            InitializeComponent();
        }

        public IOrderItem OrderItem
        {
            get { return _orderItem; }
            set
            {
                _orderItem = value;

                cbxOrderPart.SelectedItem = _orderItem != null ? _orderItem.OrderPart : null;
                cbxPriceItem.SelectedItem = _orderItem != null ? _orderItem.PriceItem : null;
            }
        }

        private void cbxOrderPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_orderItem == null)
                throw new InvalidOperationException();

            _orderItem.OrderPart = (IOrderPart)cbxOrderPart.SelectedItem;
        }

        private void cbxPriceItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_orderItem == null)
                throw new InvalidOperationException();

            _orderItem.PriceItem = (IPriceItem)cbxPriceItem.SelectedItem;
        }

        private void tbxDescription_TextChanged(object sender, EventArgs e)
        {
            if (_orderItem == null)
                throw new InvalidOperationException();

            _orderItem.Description = tbxDescription.Text;
        }
    }
}
